package org.aspect.aspect;

import org.aspect.entity.SystemLog;
import org.aspect.entity.event.SystemLogEvent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.cleaver.spring.utils.servlet.ServletUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 用户权限标注
 *
 * @author LiKun
 * @date 2021/8/20 13:19
 */
@Aspect
@Component
public class LoggerAspect {
    @Resource
    private ApplicationContext applicationContext;

    //    @Before("bean(aspectController)")
    @Before("execution(* org.aspect.controller.*.*(..))")
    public void execute(JoinPoint joinPoint) {
        HttpServletRequest request = ServletUtil.request();
        SystemLog systemLog = SystemLog.builder().ip(request.getRemoteAddr())
                .url(request.getRequestURI()).params(Arrays.toString(joinPoint.getArgs())).build();

        applicationContext.publishEvent(new SystemLogEvent(systemLog));
    }
}
